<?php

class Php_Shell{
	
	static public function Debug($variable, $indent_level = 0){

		return "=> ".self::Get_Var($variable);
	}
	
	static public function Get_Var($variable, $indent_level = 0){
		
		$type = gettype($variable);

		$out = "";
		
		switch ($type) {
			case "boolean":
				
				$out = self::Boolean($variable);
			break;
			
			case "integer":
				
				$out = self::Integer($variable);
			break;
			
			case "double":
				
				$out = self::Double($variable);
			break;
			
			case "string":
				
				$out = self::String($variable);
			break;
			
			case "NULL":
				
				$out = self::Null($variable);
			break;
			
			case "array":
			
				$out = self::Collection($variable, $indent_level);
			break;

			case "object":

				if($variable instanceof Doctrine_Record){

					$out = self::Doctrine_Record($variable);
					
            }else{
				
					// default object debug..

					$out = "#<".get_class($variable);

					$vars = array();

					foreach (get_object_vars($variable) as $prefix => $value) {

						$vars[] = "".$prefix.": ".self::Get_Var($value)."";
					}

					$out .= (empty($vars) ? "" : " ".implode(", ", $vars)).">";
            }

			break;

			default:
				echo "type not found";
			break;
		}

		return $out;
		
   }
	
	static public function Doctrine_Record($variable){
		
		$out = "#<".get_class($variable);

		$vars = array();

		foreach ($variable->_data as $prefix => $value) {

			$vars[] = "".$prefix.": ".self::Get_Var($variable->{$prefix})."";
		}

		return $out." ".implode(", ", $vars).">";
   }
	
	
	static public function Boolean($variable){
		
		return $variable ? "true" : "false";
   }
	
	static public function Integer($variable){
		
		return $variable;
   }
	
	static public function Double($variable){
		
		return $variable;
   }
	
	static public function Null($variable){
		
		return "null";
   }
	
	static public function String($variable){
		
		return "'".str_replace("'", "\\'", $variable)."'";
   }
	
	static public function Collection($variable, $indent_level = 0){

		$out = "array (\n";

		foreach ($variable as $index => $value) {

			$out .= self::_indent($indent_level + 1)."".self::Get_Var($index, $indent_level + 1)." => ".self::Get_Var($value, $indent_level + 1)."\n";
			
		}

		$out .= self::_indent($indent_level).")";

		return $out;
   }

	static protected function _indent($level){

		return str_repeat("\t", $level);
   }

}